package study.nosql.redis.support.client;

import org.springframework.data.redis.core.RedisTemplate;

/**
 * 1.静态化
 * 2.序列化可选
 */
public class RedisV2Client {

    /* static RedisTemplate */
    private static RedisTemplate<String, Object> REDIS_TEMPLATE;

    /* static RedisV1Client */
    private static RedisV2Client CLIENT = new RedisV2Client(REDIS_TEMPLATE);

    public RedisV2Client(RedisTemplate<String, Object> redisTemplate) {
        REDIS_TEMPLATE = redisTemplate;
    }

    public static <V> void set(String key, V value) {
        try {
            REDIS_TEMPLATE.opsForValue().set(key, value);
        } catch (Exception ex) {
            throw new RuntimeException("[String] set error!", ex);
        }
    }

    public static <V> V get(String key) {
        V value;
        try {
            value = (V) REDIS_TEMPLATE.opsForValue().get(key);
        } catch (Exception ex) {
            throw new RuntimeException("[String] get error!", ex);
        }
        return value;
    }
}
